/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuoc;

import java.sql.ResultSet;
import java.util.Map;
import java.util.Vector;

/**
 *
 * @author apple
 */
public class order_detail_model extends My_model{
    int id;
    int order_id;
    int product_id;
    int quantity;
    float subtotal;
    
    public Vector get_elements()
    {
        ResultSet rs = this.executeSqlString("SELECT * FROM order_detail");
        return this.to_domain_models(rs);
    }
    
    public Vector get_elements_by_order(String orderId)
    {
        ResultSet rs = this.executeSqlString("SELECT * FROM order_detail WHERE order_id = " + orderId);
        return this.to_domain_models(rs);
    }
    
    public order_detail_model get_element_by_id(String id)
    {
        ResultSet rs = this.executeSqlString("SELECT * FROM order_detail WHERE id = " + id);
        return this.to_domain_model(rs);
    }
    
    public void insert_data(Map<String,String> data)
    {
        String sqlString = String.format("INSERT INTO order_detail VALUES(NULL,%s,%s,%s,%s)",
                (data.get("order_id") != null) ? data.get("order_id") : "NULL",
                (data.get("product_id") != null) ? data.get("product_id") : "NULL",
                (data.get("quantity") != null) ? data.get("quantity") : "NULL",
                (data.get("subtotal") != null) ? data.get("subtotal") : "NULL"
                );
        this.insertDatabase(sqlString);
    }
    
//    PROPERTIES
    
    public String get_product_name()
    {
        String name = new products_model().get_element_by_id(this.product_id + "").name;
        System.out.println(name);
        return name;
    }
    
    public order_detail_model to_domain_model(ResultSet rs)
    {
        order_detail_model result = new order_detail_model();
        try {
            if(rs.next())
            {
                result.id = rs.getInt("id");
                result.order_id = rs.getInt("order_id");
                result.product_id = rs.getInt("product_id");
                result.quantity = rs.getInt("quantity");
                result.subtotal = rs.getFloat("sub_total");
            }    
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public Vector to_domain_models(ResultSet rs)
    {
        Vector result = new Vector();
        try {
            while(rs.next())
            {
                order_detail_model item = new order_detail_model();
                item.order_id = rs.getInt("order_id");
                item.product_id = rs.getInt("product_id");
                item.quantity = rs.getInt("quantity");
                item.subtotal = rs.getFloat("sub_total");
                
                result.add(item);
            }
            
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
}
